package c.j.a.d.b.e;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.SparseArray;
import c.j.a.c.y.a.i;
import c.j.a.d.b.h.c;
import java.lang.ref.WeakReference;

/* compiled from: AbsDownloadServiceHandler */
public abstract class a implements r {
    public static final String h = "a";
    public WeakReference<Service> a;
    public final SparseArray<c> b = new SparseArray();
    public volatile boolean c = false;
    public volatile boolean d;
    public volatile boolean e = false;
    public Handler f = new Handler(Looper.getMainLooper());
    public Runnable g = new a();

    /* compiled from: AbsDownloadServiceHandler */
    public class a implements Runnable {
        public void run() {
            if (c.j.a.d.b.g.a.b()) {
                c.j.a.d.b.g.a.d(a.h, "tryDownload: 2 try");
            }
            if (!a.this.c) {
                if (c.j.a.d.b.g.a.b()) {
                    c.j.a.d.b.g.a.d(a.h, "tryDownload: 2 error");
                }
                a.this.e(b.a(), null);
            }
        }
    }

    public void a(int i) {
        c.j.a.d.b.g.a.a = i;
    }

    public void b(c cVar) {
    }

    public void c() {
    }

    public void c(q qVar) {
    }

    public void d(Intent intent, int i, int i2) {
    }

    public void e(Context context, ServiceConnection serviceConnection) {
    }

    public void f(c cVar) {
        String str = h;
        StringBuilder g = c.b.a.a.a.g("pendDownloadTask pendingTasks.size:");
        g.append(this.b.size());
        g.append(" downloadTask.getDownloadId():");
        g.append(cVar.m());
        c.j.a.d.b.g.a.d(str, g.toString());
        if (this.b.get(cVar.m()) == null) {
            synchronized (this.b) {
                if (this.b.get(cVar.m()) == null) {
                    this.b.put(cVar.m(), cVar);
                }
            }
        }
        StringBuilder g2 = c.b.a.a.a.g("after pendDownloadTask pendingTasks.size:");
        g2.append(this.b.size());
        c.j.a.d.b.g.a.d(str, g2.toString());
    }

    public void g() {
        SparseArray clone;
        String str = h;
        StringBuilder g = c.b.a.a.a.g("resumePendingTask pendingTasks.size:");
        g.append(this.b.size());
        c.j.a.d.b.g.a.d(str, g.toString());
        synchronized (this.b) {
            clone = this.b.clone();
            this.b.clear();
        }
        c.j.a.d.b.j.c B = b.B();
        if (B != null) {
            for (int i = 0; i < clone.size(); i++) {
                c cVar = (c) clone.get(clone.keyAt(i));
                if (cVar != null) {
                    B.g(cVar);
                }
            }
        }
    }

    public IBinder a(Intent intent) {
        c.j.a.d.b.g.a.d(h, "onBind Abs");
        return new Binder();
    }

    public void a(c cVar) {
        if (cVar != null) {
            if (this.c) {
                if (this.b.get(cVar.m()) != null) {
                    synchronized (this.b) {
                        if (this.b.get(cVar.m()) != null) {
                            this.b.remove(cVar.m());
                        }
                    }
                }
                c.j.a.d.b.j.c B = b.B();
                if (B != null) {
                    B.g(cVar);
                }
                g();
            } else {
                if (c.j.a.d.b.g.a.b()) {
                    c.j.a.d.b.g.a.d(h, "tryDownload but service is not alive");
                }
                if (i.s(262144)) {
                    synchronized (this.b) {
                        f(cVar);
                        if (this.e) {
                            this.f.removeCallbacks(this.g);
                            this.f.postDelayed(this.g, 10);
                        } else {
                            if (c.j.a.d.b.g.a.b()) {
                                c.j.a.d.b.g.a.d(h, "tryDownload: 1");
                            }
                            e(b.a(), null);
                            this.e = true;
                        }
                    }
                } else {
                    f(cVar);
                    e(b.a(), null);
                }
            }
        }
    }

    public void f() {
        if (!this.c) {
            if (c.j.a.d.b.g.a.b()) {
                c.j.a.d.b.g.a.d(h, "startService");
            }
            e(b.a(), null);
        }
    }
}
